class SallesController < ApplicationController
before_filter :login_required, :only => [:new, :edit, :create, :update, :destroy]
  
  
  # GET /salles
  # GET /salles.xml
  def index
    @salles = Salle.all

    respond_to do |format|
      format.html {render :layout => 'layout'} # index.html.erb
      format.xml  { render :xml => @salles }
    end
  end

  # GET /salles/1
  # GET /salles/1.xml
  def show
    @salle = Salle.find(params[:id])

    respond_to do |format|
      format.html {render :layout => 'layout'} # show.html.erb
      format.xml  { render :xml => @salle }
    end
  end

  # GET /salles/new
  # GET /salles/new.xml
  def new
    @salle = Salle.new

    respond_to do |format|
      format.html {render :layout => 'layout'} # new.html.erb
      format.xml  { render :xml => @salle }
    end
  end

  # GET /salles/1/edit
  def edit
    @salle = Salle.find(params[:id])
    respond_to do |format|
      format.html {render :layout => 'layout'} # new.html.erb
      format.xml  { render :xml => @salle }
    end
  end

  # POST /salles
  # POST /salles.xml
  def create
    @salle = Salle.new(params[:salle])

    respond_to do |format|
      if @salle.save
        flash[:notice] = 'La salle a été créée.'
        format.html { redirect_to('/homes') }
        format.xml  { render :xml => @salle, :status => :created, :location => @salle }
      else
        format.html { render :action => "new", :layout => 'layout' }
        format.xml  { render :xml => @salle.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /salles/1
  # PUT /salles/1.xml
  def update
    @salle = Salle.find(params[:id])

    respond_to do |format|
      if @salle.update_attributes(params[:salle])
        flash[:notice] = 'La salle a été mise à jour.'
        format.html { redirect_to(@salle) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit", :layout => 'layout' }
        format.xml  { render :xml => @salle.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /salles/1
  # DELETE /salles/1.xml
  def destroy
    @salle = Salle.find(params[:id])
	#@salle.delete_salle_update_machines()
	@salle.machines.each do |machine|
		machine.update_attribute("salle_id", 1)
	end
    @salle.destroy

    respond_to do |format|
      format.html { redirect_to(salles_url) }
      format.xml  { head :ok }
    end
  end
end
